package com.ruoyi.web.controller.level;

import java.util.List;
import java.util.UUID;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.level.domain.PointsLevelRule;
import com.ruoyi.level.service.IPointsLevelRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 用户积分等级规则Controller
 * 
 * @author 区嘉盛
 * @date 2025-06-16
 */
@RestController
@RequestMapping("/level/level")
@Tag(name = "【用户积分等级规则】管理")
public class PointsLevelRuleController extends BaseController
{
    @Autowired
    private IPointsLevelRuleService pointsLevelRuleService;

    /**
     * 查询用户积分等级规则列表
     */
    @Operation(summary = "查询用户积分等级规则列表")
    @GetMapping("/list")
    public TableDataInfo list(PointsLevelRule pointsLevelRule)
    {
        startPage();
        List<PointsLevelRule> list = pointsLevelRuleService.selectPointsLevelRuleList(pointsLevelRule);
        return getDataTable(list);
    }

    /**
     * 导出用户积分等级规则列表
     */
    @Operation(summary = "导出用户积分等级规则列表")
    @PreAuthorize("@ss.hasPermi('level:level:export')")
    @Log(title = "用户积分等级规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PointsLevelRule pointsLevelRule)
    {
        List<PointsLevelRule> list = pointsLevelRuleService.selectPointsLevelRuleList(pointsLevelRule);
        ExcelUtil<PointsLevelRule> util = new ExcelUtil<PointsLevelRule>(PointsLevelRule.class);
        util.exportExcel(response, list, "用户积分等级规则数据");
    }

    /**
     * 获取用户积分等级规则详细信息
     */
    @Operation(summary = "获取用户积分等级规则详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pointsLevelRuleService.selectPointsLevelRuleById(id));
    }

    /**
     * 新增用户积分等级规则
     */
    @Operation(summary = "新增用户积分等级规则")
    @PreAuthorize("@ss.hasPermi('level:level:add')")
    @Log(title = "用户积分等级规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PointsLevelRule pointsLevelRule)
    {
        if (pointsLevelRuleService.checkLevelNameExists(pointsLevelRule.getSystemName(), pointsLevelRule.getLevelName()))
        {
            return error("该等级体系已有此等级");
        }
        if (pointsLevelRuleService.checkSortOrderExists(pointsLevelRule.getSystemName(), pointsLevelRule.getSortOrder()))
        {
            return error("该等级体系已有此排序号");
        }
        String checkResult = pointsLevelRuleService.checkPointsRangeValid(
                pointsLevelRule.getSystemName(),
                pointsLevelRule.getSortOrder(),
                pointsLevelRule.getMinPoints(),
                pointsLevelRule.getMaxPoints()
        );
        if (checkResult != null)
        {
            return error(checkResult);
        }
        pointsLevelRule.setLevelStatus(1L);
        pointsLevelRule.setCreatedBy(getUsername());
        pointsLevelRule.setSystemCode(UUID.randomUUID().toString().replace("-", ""));
        pointsLevelRule.setLevelCode(UUID.randomUUID().toString().replace("-", ""));
        return toAjax(pointsLevelRuleService.insertPointsLevelRule(pointsLevelRule));
    }

    /**
     * 修改用户积分等级规则
     */
    @Operation(summary = "修改用户积分等级规则")
    @PreAuthorize("@ss.hasPermi('level:level:edit')")
    @Log(title = "用户积分等级规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PointsLevelRule pointsLevelRule)
    {
        if (pointsLevelRuleService.checkLevelNameExistsExcludeId(pointsLevelRule.getSystemName(), pointsLevelRule.getLevelName(), pointsLevelRule.getId()))
        {
            return error("该等级体系已有此等级");
        }
        if (pointsLevelRuleService.checkSortOrderExistsExcludeId(pointsLevelRule.getSystemName(), pointsLevelRule.getSortOrder(), pointsLevelRule.getId()))
        {
            return error("该等级体系已有此排序号");
        }
        String checkResult = pointsLevelRuleService.checkPointsRangeValid(
                pointsLevelRule.getSystemName(),
                pointsLevelRule.getSortOrder(),
                pointsLevelRule.getMinPoints(),
                pointsLevelRule.getMaxPoints()
        );
        if (checkResult != null)
        {
            return error(checkResult);
        }
        pointsLevelRule.setUpdatedBy(getUsername());
        return toAjax(pointsLevelRuleService.updatePointsLevelRule(pointsLevelRule));
    }

    /**
     * 停用用户积分等级规则
     */
    @Operation(summary = "停用用户积分等级规则")
    @PreAuthorize("@ss.hasPermi('level:level:remove')")
    @Log(title = "用户积分等级规则", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable( name = "ids" ) Long[] ids)
    {
        return toAjax(pointsLevelRuleService.disablePointsLevelRuleByIds(ids));
    }

    /**
     * 启用用户积分等级规则
     */
    @Operation(summary = "启用用户积分等级规则")
    @PreAuthorize("@ss.hasPermi('level:level:remove')")
    @Log(title = "用户积分等级规则", businessType = BusinessType.UPDATE)
    @PutMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable( name = "ids" ) Long[] ids) 
    {
        return toAjax(pointsLevelRuleService.enablePointsLevelRuleByIds(ids));
    }
}
